﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.Extensions;
using VOL.Entity.DomainModels;
using VOL.SmartHealthcare.Infrastructure;
using VOL.Team.TeamIServices.ProductManagement.IServices;

namespace VOL.Team.TeamIServices.ProductManagement.Services
{
    /// <summary>
    /// 产品管理
    /// </summary>
    public class ProductMaintenanceServices: IProductMaintenanceServices
    {
        /// <summary>
        /// 产品管理-产品维护
        /// </summary>
        private readonly IBaseRepository<ProductMaintenance> _productmaintenanceRepository;

        public ProductMaintenanceServices(IBaseRepository<ProductMaintenance> productmaintenanceRepository)
        {
            _productmaintenanceRepository = productmaintenanceRepository;
        }
        /// <summary>
        /// 查询产品维护
        /// </summary>
        /// <returns></returns>
        public IQueryable<ProductMaintenance> GetProductMaintenance(string? qecode, string? bomcode, string? productcode)
        {
            try
            {
                var productmaintenanceList = _productmaintenanceRepository.GetAll();
                productmaintenanceList = productmaintenanceList.WhereIF(qecode != null, m => m.QRCode == qecode);
                productmaintenanceList = productmaintenanceList.WhereIF(bomcode != null, m => m.BOMCode == bomcode);
                productmaintenanceList = productmaintenanceList.WhereIF(productcode != null, m => m.ProductCode == productcode);
                return productmaintenanceList;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 新增物料台账-xzc
        /// </summary>
        /// <param name="productmaintenance"></param>
        /// <returns></returns>
        public async Task<int> AddProductMaintenance(ProductMaintenance productmaintenance)
        {
            try
            {
                var productmaintenanceList = await _productmaintenanceRepository.AddAsync(productmaintenance);
                return productmaintenanceList;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 更新产品维护
        /// </summary>
        /// <param name="productmaintenance"></param>
        /// <returns></returns>
        public async Task<int> UpdateProductMaintenance(ProductMaintenance productmaintenance)
        {
            try
            {
                var productmaintenanceList = await _productmaintenanceRepository.UpdateAsync(productmaintenance);
                return productmaintenanceList;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 删除产品维护
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteProductMaintenance(int id)
        {
            try
            {
                var productmaintenanceList = await _productmaintenanceRepository.DeleteAsync(id);
                return productmaintenanceList;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 批量删除产品维护
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<int> DeleteAnageProductMaintenance(string Id)
        {
            try
            {
                string[] pid = Id.Split(',');
                var a = 0;
                foreach (var item in pid)
                {
                    a = await _productmaintenanceRepository.DeletteRangeAsync(m => m.ProductMaintenanceId == Convert.ToInt32(item));
                }
                return a;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
